package com.ezcoding.engine;

import com.ezcoding.model.*;
import com.ezcoding.utils.StatusUtil;
import com.ezcoding.utils.Tools;

import java.util.List;
import java.util.Map;

public class SaleOutCalculator extends BaseDingdanCalculator {

    public SaleOutCalculator(Dingdan dingdan, List<DingdanDetail> details) {
        super(dingdan, details);
    }

    @Override
    public boolean checkProductStore(int curCount, int needCount) {
        return curCount >= needCount;
    }

    @Override
    public double getCurTicket(double curValue) {
        if (dingdan.getIsTicket() == 1) {
            //不开票
            return 0;
        }

        return curValue;
    }

    @Override
    public double getCurMoney(double curValue) {
        return curValue;
    }

    @Override
    public double getTotalMoney() {
        return dingdan.getSourceFreightUnpaid() - dingdan.getTargetFreightPaid() + dingdan.getFreeMoney() + dingdan.getMoney();
    }

    @Override
    public void updateCompanyInfo() {
        Company company = getCompany();

        company.setTotalTicket(company.getTotalTicket() - dingdan.getTicket());
        company.setTotalMoney(company.getTotalMoney() - dingdan.getSourceFreightPaid() - dingdan.getTargetFreightPaid() + dingdan.getMoney());
    }

    @Override
    public Map<String, Object> updateRelatedDingdanInfo() {
        return StatusUtil.getSuccessStatusWithEmptyMessage();
    }

    @Override
    public void setDetailTicket(DingdanDetail detail, double value) {
        detail.setTicketOut(value);
    }

    @Override
    public void setDetailMoney(DingdanDetail detail, double value) {
        detail.setMoneyIn(value);
    }

    @Override
    public void setTicket(double value) {
        dingdan.setTicketOut(value);
    }

    @Override
    public void setMoney(double value) {
        dingdan.setMoneyIn(value);
    }

    @Override
    public int getRelationType() {
        return UserRelation.RELATION_TYPE_CLIENT;
    }

    @Override
    public void updateProductStore(Product product, int needCount) {
        product.setCount(product.getCount() - needCount);
    }

    @Override
    public void updateProductAndDetail(Product product, DingdanDetail detail) {
        updateProfit(detail, product.getInPrice());
        updateProductStore(product, detail.getCount());
    }

    @Override
    public void updateProfit(DingdanDetail detail, double inPrice) {
        double profit = (detail.getPrice() / getTaxRate() - inPrice) * detail.getCount();
        detail.setProfit(Tools.formatDouble(profit));
    }

    @Override
    public void updateProductInPrice(Product product, DingdanDetail detail) {

    }
}
